package com.reggie.serve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.serve.common.CustomException;
import com.reggie.serve.entity.Category;
import com.reggie.serve.entity.Dish;
import com.reggie.serve.entity.SetMeal;
import com.reggie.serve.mapper.CategoryMapper;
import com.reggie.serve.service.CategoryService;
import com.reggie.serve.service.DishService;
import com.reggie.serve.service.SetMealService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    private final DishService dishService;

    private final SetMealService setmealService;

    /**
     * 根据id删除分类，删除之前需要进行判断
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int dishCount = dishService.count(dishLambdaQueryWrapper);
        if (dishCount > 0) {
            //当前分类下有菜品，不能删除
            throw new CustomException("当前分类下关联了菜品,不能删除");
        }
        LambdaQueryWrapper<SetMeal> setMealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setMealLambdaQueryWrapper.eq(SetMeal::getCategoryId, id);
        int setMealCount = setmealService.count(setMealLambdaQueryWrapper);
        if (setMealCount > 0) {
            //当前分类下有套餐，不能删除
            throw new CustomException("当前分类下关联了套餐,不能删除");
        }
        super.removeById(id);
    }
}
